<?php

class AppModel extends Model
{
	public function getapplist()
	{
		$app = C('app_list');
		foreach ($app as &$v) {
			foreach ($v['version'] as &$version) {
				$version['vname'] = $version['name'];
				$version['name'] .= '(价格：' . rmbFormat($version['price'], $v['deadline']) . ')';
				foreach ($version['service'] as $year => $service) {
					if ($year == 100) {
						$version['serviceoutput'][$year] = '附加服务期限：永久 (价格：' . rmbFormat($service, $v['deadline']) . ')';
						$version['servicenoprice'][$year] = '附加服务期限：永久';
					} elseif ($year == 0) {
						$version['serviceoutput'][$year] = '不延长服务期限';
						$version['servicenoprice'][$year] = '不延长服务期限';
					} else {
						$version['serviceoutput'][$year] = '附加服务期限：' . $year . $v['deadline'] . ' (价格：' . rmbFormat($service, $v['deadline']) . ')';
						$version['servicenoprice'][$year] = '附加服务期限：' . $year . $v['deadline'] ;
					}
				}
			}
		}
		return $app;
	}

	public function create($param)
	{
		$app=$this->getapplist();
		$order['user_id']=cookie('userid');
		$order['type']='app';
		$order['type_id']='0';
		$order['create_time']=NOW_TIME;
		$order['title']=$param['name'].'('.$app[$param['app']]['name'].'['.$app[$param['app']]['version'][$param['version']]['vname'].']'.')';
		$order['price']=$app[$param['app']]['version'][$param['version']]['price'];
		$order['addon']=$app[$param['app']]['version'][$param['version']]['serviceoutput'][$param['service']];
		$order['addonprice']=$app[$param['app']]['version'][$param['version']]['service'][$param['service']];
		$order['totalprice']=$order['price']+$order['addonprice'];
		$order['status']=0;
		$orderid=M('order')->insert($order);
		if ($orderid){
			$appinfo['secret']=md5(md5($_SERVER['REQUEST_TIME']).$orderid);
			$appinfo['name']=$param['name'];
			$appinfo['program']=$param['app'];
			$appinfo['version']=$param['version'];
			$appinfo['service']=$param['service'];
			$appinfo['intro']=$param['intro'];
			$appinfo['order_id']=$orderid;
			$appinfo['user_id']=$order['user_id'];
			$appinfo['create_time']=NOW_TIME;
			$appinfo['end_time']=$app[$param['app']]['deadline']=='年'?strtotime('+'.$param['service'].' year'):strtotime('+'.$param['service'].' month');
			$appid=$this->insert($appinfo);
			M('order')->where(array('id'=>$orderid))->update(array('type_id'=>$appid));
			return M('order')->genorderid($orderid,NOW_TIME);
		}
		return false;
	}

	public function getlist()
	{
		$list=(array)$this->select();
		foreach($list as &$v){
			$v=$this->dataAppend($v);
		}
		return $list;
	}

	public function dataAppend($arr)
	{
		$app = $this->getapplist();
		$arr['app']=$arr['program'];
		$arr['service']=$app[$arr['program']]['version'][$arr['version']]['servicenoprice'][$arr['service']];
		$arr['limit']=$app[$arr['program']]['version'][$arr['version']]['limit'];
		$arr['version']=$app[$arr['program']]['version'][$arr['version']]['vname'];
		$arr['program']=$app[$arr['program']]['name'];
		$arr['order_id']=M('order')->genorderid($arr['order_id'],$arr['create_time']);
		$arr['app_id']=$this->genappid($arr['id']);
		return $arr;
	}

	public function genappid($id)
	{
		return '1'.str_pad($id*3+423,9,'0',STR_PAD_LEFT);
	}

	public function decodeappid($str)
	{
		return (intval(substr($str,1))-423)/3;
	}

	public function getinfo($appid)
	{
		$id=$this->decodeappid($appid);
		return $this->dataAppend($this->where(array('id'=>$id))->find());
	}
}